package cn.edu.ctbu.sbadmin.lablog.webapi;

import cn.edu.ctbu.sbadmin.common.controller.BaseRestController;
import cn.edu.ctbu.sbadmin.common.utils.MQuery;
import cn.edu.ctbu.sbadmin.common.utils.MQueryHelper;
import cn.edu.ctbu.sbadmin.common.utils.PageUtils;
import cn.edu.ctbu.sbadmin.common.utils.R;
import cn.edu.ctbu.sbadmin.lablog.domain.DimTermDO;
import cn.edu.ctbu.sbadmin.lablog.service.DimTermService;
import cn.edu.ctbu.sbadmin.system.domain.RoleDO;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/webapi/lablog/dimterm")  //先访问这个
public class DimTermRestController  extends BaseRestController {
    @Autowired
    DimTermService dimTermService;
    //    @RequiresPermissions("system:role:list")
    @RequestMapping("/getByPage")   // 再访问这个   会拼接成/webapi/lablog/dimterm/getByPage
    //lablog（模块）:dimterm（控制器）:list（方法）
    @RequiresPermissions("lablog:dimterm:list")  //读权限（约定为读权限）必须含有lablog:dimter:list的权限才能执行方法PageUtils list
    /**
     * where查询的条件
     * pageSize每页多少数据
     * pageIndex当前页
     * sort排序
     */
    public PageUtils list(@RequestParam("where") String where,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("pageIndex") Integer pageIndex,
                          @RequestParam("sort") String sort,
                          @RequestParam("direct") String direct
    ) throws Exception {
        //首先，把where条件的参数转为QueryAndItems
//        其中,where条件需要我们自己组装.示例:
//        name=张^:like&time=2^:lg&dmtCreate=2017-01-01^^2018-01-01
//        这个表示:
//        (name like '%张%' )and ( time<2 ) and (dmtCreate between '2017-01-01' and '2018-01-01)
        MQuery mQuery= MQueryHelper.GenQuery(where,pageSize,pageIndex,sort,direct);//封装成对象
        //这边按ctrl点击代码，便可以查看代码
        PageInfo<DimTermDO> dimTermDOPageInfo = dimTermService.getByPage(mQuery);

        PageUtils pageUtils = new PageUtils(dimTermDOPageInfo.getList(),dimTermDOPageInfo.getTotal());

        return pageUtils;
    }

    /**
     * 根据id读取数据
     * @param id
     * @return
     */
    @RequestMapping("/get")   //取数据
    @RequiresPermissions("lablog:dimterm:list")  //有权限才能访问
    public R get(Long id) {
        DimTermDO dimTermDO = dimTermService.get(id);
        if (dimTermDO != null) {
            return R.ok("data", dimTermDO);
        }
        return R.error(-1, "出错了，找不到此数据!");
    }

    /**
     * 保存数据
     * @param dimTermDO
     * @return
     */
    @PostMapping("/save")
    @RequiresPermissions("lablog:dimterm:add")
    public R save(DimTermDO dimTermDO) {
        //save有返回值，返回大于0则是save成功
        if (dimTermService.save(dimTermDO) > 0) {
            return R.ok("data", dimTermDO);
        }
        return R.error();
    }

    /**
     * 更新方法
     * @param dimTermDO
     * @return
     */
    @PostMapping("/update")
    @RequiresPermissions("lablog:dimterm:edit") //可编辑
    public R updata(DimTermDO dimTermDO) {
        //save有返回值，返回大于0则是save成功
        if (dimTermService.update(dimTermDO) > 0) {
            return R.ok("data", dimTermDO);
        }
        return R.error();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("/batchDelete")
    @RequiresPermissions("lablog:dimterm:remove")
    public R batchDelete(@RequestParam("ids[]") Long[] ids)
    {
        //先读取再删除
        List<DimTermDO> dimTermDOS=dimTermService.findByIds(ids);
        if(dimTermDOS.size()>0)
        {
            //依次循环删除
            for(DimTermDO dimTermDO:dimTermDOS)
            {
                dimTermService.delete(dimTermDO.getId());
            }
        }
        return R.ok();
    }
    @RequestMapping("/getAll")   //取数据
    @RequiresPermissions("lablog:dimterm:list")  //有权限才能访问
    public R getAll() {
        List<DimTermDO> dimTermDOS = dimTermService.findAll();
        return R.ok("data", dimTermDOS);
    }
}
